package com.android.game.lib.graphics;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;

import android.opengl.GLSurfaceView;
import android.util.Log;

public class GL2ContextFactory implements GLSurfaceView.EGLContextFactory{
	private static int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
	private static String TAG = "GL2_CONTEXT";
	
	private static void CheckEglError(String prompt, EGL10 egl){
		int error;
	    while ((error = egl.eglGetError()) != EGL10.EGL_SUCCESS) {
	        Log.e(TAG, String.format("%s: EGL error: 0x%x", prompt, error));
	    }
	}
	
	@Override
	public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig) {
		// TODO Auto-generated method stub
		Log.w(TAG, "Create OpenGL ES 2.0 context");
		CheckEglError("Before elgCreateContext", egl);
		int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE};
		EGLContext context = egl.eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list);
		CheckEglError("After eglCreateContext", egl);
		return context;
	}
	
	@Override
	public void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context) {
		// TODO Auto-generated method stub
		egl.eglDestroyContext(display, context);
	}
}
